package com.aidysoft.practice.easypoi.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import cn.afterturn.easypoi.word.WordExportUtil;
import com.aidysoft.practice.easypoi.utils.pojo.ImportForMap.MapImportHandler;
import com.aidysoft.practice.easypoi.utils.style.ExcelStyleUtil;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class ExcelUtil {
    /**
     * 非模板 单sheet
     * 传递
     * 对象集合
     * 是否加表头 默认 true
     *
     * @param list
     * @param title
     * @param sheetName
     * @param pojoClass
     * @param fileName
     * @param isCreateHeader
     * @param response
     */
    public static void exportExcel
    (
            List<?> list,
            String title,
            String sheetName,
            Class<?> pojoClass,
            String fileName,
            boolean isCreateHeader,
            HttpServletResponse response
    ) {
        ExportParams exportParams = null;
        if (StringUtils.contains(fileName, ".xlsx")) {
            exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
        } else {
            exportParams = new ExportParams(title, sheetName, ExcelType.HSSF);
        }
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);

    }

    /**
     * 非模板 单sheet
     * 传递
     * 对象集合
     *
     * @param list
     * @param title
     * @param sheetName
     * @param pojoClass
     * @param fileName
     * @param response
     */
    public static void exportExcel
    (
            List<?> list,
            String title,
            String sheetName,
            Class<?> pojoClass,
            String fileName,
            HttpServletResponse response
    ) {
        ExportParams exportParams = null;
        //根据fileNme后缀导出相应类型的excel文件
        if (StringUtils.contains(fileName, ".xlsx")) {
            exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
        } else {
            exportParams = new ExportParams(title, sheetName, ExcelType.HSSF);
        }
        exportParams.setStyle(ExcelStyleUtil.class);//设置样式，可自定义
        defaultExport(list, pojoClass, fileName, response, exportParams);
    }

    /**
     * map
     * <p>
     * 非模板 多sheet
     *
     * @param list
     * @param fileName
     * @param response
     */

    public static void exportExcel
    (
            List<Map<String, Object>> list,
            String fileName,
            HttpServletResponse response
    ) {
        defaultExport(list, fileName, response);
    }

    /**
     * 对象
     * <p>
     * 通用导出
     *
     * @param list
     * @param pojoClass
     * @param fileName
     * @param response
     * @param exportParams
     */
    private static void defaultExport
    (
            List<?> list,
            Class<?> pojoClass,
            String fileName,
            HttpServletResponse response,
            ExportParams exportParams
    ) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        if (workbook != null) ;
        downLoadExcel(fileName, response, workbook);
    }

    /**
     * map
     * <p>
     * 模板  单sheet
     *
     * @param map
     * @param params
     * @param fileName
     * @param response
     */
    public static void exportTemplateExcel
    (
            Map<String, Object> map,
            TemplateExportParams params,
            String fileName,
            HttpServletResponse response
    ) {
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        if (workbook != null) ;
        downLoadExcel(fileName, response, workbook);
    }

    /**
     * 通用 下载
     *
     * @param fileName
     * @param response
     * @param workbook
     */
    public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

            workbook.write(response.getOutputStream());
        } catch (IOException e) {
//            throw new BusinessException(e.getMessage(), false);
        }
    }

    /**
     * map
     * 非模板 多 sheet
     *
     * @param list
     * @param fileName
     * @param response
     */
    private static void defaultExport
    (
            List<Map<String, Object>> list,
            String fileName,
            HttpServletResponse response
    ) {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null) ;
        downLoadExcel(fileName, response, workbook);
    }

    /**
     * 导入
     * <p>
     * 路径
     *
     * @param filePath
     * @param titleRows
     * @param headerRows
     * @param pojoClass
     * @param <T>
     * @return
     */
    public static <T> List<Map<String, Object>> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setDataHandler(new MapImportHandler());

        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<Map<String, Object>> list = null;
        try {
             list=ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
            System.out.println(list);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;
    }

    /**
     * 导入
     * 文件
     *
     * @param file
     * @param titleRows
     * @param headerRows
     * @param pojoClass
     * @param <T>
     * @return
     */
    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list;
    }

    /**
     * 封装 map
     *
     * @param entityList
     * @param data
     * @param exportParams
     * @return
     */
    public static Map<String, Object> getExcelMap(List<ExcelExportEntity> entityList, List<Map<String, Object>> data, ExportParams exportParams) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("title", exportParams);
        map.put("entity", entityList);
        map.put("data", data);
        return map;
    }


    /**
     * map 导出 非欧模板
     *
     * @param exportParams
     * @param entityList
     * @param mapList
     * @param type
     * @return
     */
    public static Workbook exportExcel(ExportParams exportParams, List<ExcelExportEntity> entityList, Collection<?> mapList, ExcelType type) {
        Workbook workbook = getWorkbook(type, 0);
        new ExcelExportService().createSheetForMap(workbook, exportParams, entityList, mapList);
        return workbook;
    }

    private static Workbook getWorkbook(ExcelType type, int size) {
        if (ExcelType.HSSF.equals(type)) {
            return new HSSFWorkbook();
        } else if (size < 100000) {
            return new XSSFWorkbook();
        } else {
            return new SXSSFWorkbook();
        }
    }
}